/*
 * teste_open.c (Refer�ncia: plano_teste.doc item 4.1)
 *
 * Verificar se a função open retorna erro na tentativa de abertura de um diretorio inexistente.
 *
 *  Created on: Oct 26, 2009
 *      Author: giovani
 */

#include <fs.h>
#include <pcb.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

PCB pcb;

int main(){

	int fd = open("/diretorioinexistente/teste.txt", "r");

	//Imprimindo informa��es do descritor
	if(fd == -1){
		printf("Erro ao abrir arquivo, errono:%d\n",errno);
	}
	else{
		printf("###### Retorno #######\n");
	
		printf("retorno: %d \n", fd);
	
		printf("errno: %d \n", errno);
	
		printf("###### Descritor ######\n");
	
		printf("nr descritor: %d \n", fd);
	
		printf("usado: %s \n", pcb.descriptors[fd].usado ? "SIM" : "NAO");
	
		printf("ptr_taa: %u \n", pcb.descriptors[fd].ptr_fentry);
	
		printf("endereco_buffer: %u \n", pcb.descriptors[fd].buffer);
	
		printf("mode: %d \n", pcb.descriptors[fd].mode);
	
		printf("posicao_arquivo: %d \n", pcb.descriptors[fd].fpos);
	
		printf("nr_bloco_buffer: %d \n", pcb.descriptors[fd].buffer_block);
	
		printf("changed: %d \n", pcb.descriptors[fd].buffer_changed);
	
		printf("##### TAA #####\n");
	
		printf("fcount: %d \n", pcb.descriptors[fd].ptr_fentry->fcount);
	
		printf("inode: %d \n", pcb.descriptors[fd].ptr_fentry->inode_num);
	
		printf("##### inode #####\n");
	
		printf("tipo_arquivo: %d \n", pcb.descriptors[fd].ptr_fentry->f_inode.tipo_arquivo);
	
		printf("tamanho: %d \n", pcb.descriptors[fd].ptr_fentry->f_inode.f_size);
	}

	return 0;
}

typedef struct{
  char name[32];   //nome de elemento do diret�rio.
  int f_inode;     //n�mero do i-node do arquivo.
} d_name;

int carrega_bloco_no_buffer(int end_bloco, void* ptr_buffer){

	int i = 0;

	switch(end_bloco){
		case 0:
			//Carrego o super-bloco
			break;
		case 1:
			//Primeiro bloco de inodes

			inode inode0;

			inode0.direct_blocks[0] = 600;

			for(i = 1; i < 10; i++){
				inode0.direct_blocks[i] = 0;
			}

			inode0.single_indirection = 0;
			inode0.double_indirection = 0;
			inode0.triple_indirection = 0;

			inode0.f_owner = 1;

			inode0.f_group = 1;

			inode0.f_access_date = 0;
			inode0.f_change_date = 0;
			inode0.f_create_date = 0;

			inode0.f_permissions = OWNER_WRITE_PERMISSION | OWNER_READ_PERMISSION | OWNER_EXECUTE_PERMISSION;

			inode0.tipo_arquivo = 'd';

			inode0.f_size = 108;

			inode inode1;

			inode1.direct_blocks[0] = 604;

			for(i = 1; i < 10; i++){
				inode1.direct_blocks[i] = 0;
			}

			inode1.single_indirection = 0;
			inode1.double_indirection = 0;
			inode1.triple_indirection = 0;

			inode1.f_owner = 1;

			inode1.f_group = 1;

			inode1.f_access_date = 0;
			inode1.f_change_date = 0;
			inode1.f_create_date = 0;

			inode1.f_permissions = OWNER_WRITE_PERMISSION | OWNER_READ_PERMISSION | OWNER_EXECUTE_PERMISSION;

			inode1.tipo_arquivo = 'r';

			inode1.f_size = 1024;

			memcpy(ptr_buffer, &inode0, sizeof(inode));

			memcpy(((char*)ptr_buffer)+sizeof(inode), &inode1, sizeof(inode));

			break;

		case 600:

			d_name diretorios[3];

			strcpy(diretorios[0].name,".");
			diretorios[0].f_inode = 0;

			strcpy(diretorios[1].name,"..");
			diretorios[1].f_inode = 0;

			strcpy(diretorios[2].name, "teste.txt");
			diretorios[2].f_inode = 1;

			memcpy(ptr_buffer, &diretorios, 3*sizeof(d_name));

		break;

		default:
			memset(ptr_buffer, 5, BLOCK_SIZE);
	}

}

int alloc_taa_entry(fentry** entry, ftable* table, int inode){

	*entry = (fentry*) malloc(sizeof(fentry));

	//TODO inserir entry na tabela
	memset(*entry, 0, sizeof(fentry));

	return 0;
}

int get_taa_entry(fentry** entry, ftable* table, int inode){
	return -1;
}

int dealloc_taa_entry(fentry* entry, ftable* table){

	//Remove da lista de table o entry
	//TODO

	//Libera mem�ria
	free(entry);

	return 0;
}

